package lium.lbs.lbsim.pay

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.support.annotation.IdRes
import android.view.View
import android.widget.RadioGroup
import com.buz.hjcdriver.bean.AResultBean
import com.lmlibrary.Constants
import com.lmlibrary.bean.ResponseBean
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_choose_pay_way.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.activity.BaseActivity
import lium.lbs.lbsim.activity.SetGroupActivity
import lium.lbs.lbsim.jgchat.application.GJApp
import lium.lbs.lbsim.jgchat.utils.photovideo.takevideo.utils.LogUtils
import lium.lbs.lbsim.listener.PayMessageEven
import lium.lbs.lbsim.listener.ReMarketMessageEven
import lium.lbs.lbsim.pay.alipayutils.ZFBActivity
import lium.lbs.lbsim.pay.weixinpay.WXPayActivity
import lium.lbs.lbsim.utils.SpUtils
import lium.lbs.lbsim.utils.ToastUtils
import lium.lbs.lbsim.utils.callbck.DialogCallback
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.HashMap

class ChoosePayWayActivity : BaseActivity() {

    var chooseNumber: Int = 1
    var name: String? = null
    var orderSN: String? = null
    var needMoney: String? = null
    var type: Int = 0

    override fun getLayoutId(): Int {
        return R.layout.activity_choose_pay_way
    }


    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("支付")

        name = intent.getStringExtra("name")
        needMoney = intent.getStringExtra("moeny")
        orderSN = intent.getStringExtra("orderSN")
        type = intent.getIntExtra("type",0)

        if (!name.equals("充值")) {
            yue.visibility = View.VISIBLE
            yue.isChecked = true
            chooseNumber = 1
        } else {
            yue.visibility = View.GONE
            zfb.isChecked = true
            chooseNumber = 2
        }
        addOnClickListeners(R.id.btn_creategroup_pay)
        paymoeny.text = "￥" + needMoney
        tochoose.setOnCheckedChangeListener(
            object : RadioGroup.OnCheckedChangeListener {
                override fun onCheckedChanged(group: RadioGroup, @IdRes checkedId: Int) {
                    when (checkedId) {
                        R.id.yue -> {
                            chooseNumber = 1
                        }
                        R.id.zfb -> {
                            chooseNumber = 2
                        }
                        R.id.wx -> {
                            chooseNumber = 3
                        }
                    }
                }
            })
    }


    override fun onClick(v: View?) {
        super.onClick(v)
        when (v!!.id) {
            R.id.btn_creategroup_pay -> {
                when (chooseNumber) {
                    1 -> {
                        if(type == 1){
                            payYue2()
                        } else {
                            payYue()
                        }

                    }
                    2 -> {
                        intent.setClass(this, ZFBActivity::class.java)
                        intent.putExtra("orderSN", orderSN)
                       // intent.putExtra("needMoney", needMoney)
                        intent.putExtra("needMoney", needMoney)
                        intent.putExtra("title", name)
                        startActivityForResult(intent, 100)

                    }
                    3 -> {
                        intent.setClass(this, WXPayActivity::class.java)
                        intent.putExtra("orderSN", orderSN)
                        intent.putExtra("needMoney", needMoney)
                        //intent.putExtra("needMoney", needMoney)
                        intent.putExtra("title", name)
                        SpUtils.getInstance().put(Constants.WXPAYRE, false)
                        startActivityForResult(intent, 100)
                    }

                }
            }

        }
    }

    override fun getNetWorkData() {

    }

    //支付宝 结果
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (resultCode == 200) {

            if (type == 1){
                setResult(200)
            } else {
                setEstablishGroup()
            }

            finish()

        } else if (resultCode == 250) {
            finish()
            if (type == 1){
                setResult(200)
            } else{
                setEstablishGroup()
            }
        }

    }

    fun payYue() {
        val param = HashMap<String, String>()
        param["memberId"] = SpUtils.getInstance().get(Constants.memberId, "") as String
        param["orderSn"] = orderSN.toString()
        param["price"] = needMoney.toString()

        postData(
            "/group/balance",
            param,
            object : DialogCallback<ResponseBean<AResultBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                    setDatapayYue(response.body().msg)
                }
            })
    }

    fun payYue2() {
        val param = HashMap<String, String>()
        param["memberId"] = SpUtils.getInstance().get(Constants.memberId, "") as String
        param["orderSn"] = orderSN.toString()
        param["price"] = needMoney.toString()

        postData(
            "/groupmember/balance",
            param,
            object : DialogCallback<ResponseBean<AResultBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<AResultBean>>) {
                    setDatapayYue(response.body().msg)
                    setResult(200)
                }
            })
    }


    fun setDatapayYue(msg: String) {
        ToastUtils.showLongToast(msg)
        setEstablishGroup()
        finish()
    }

    fun setEstablishGroup() {
        intent.setClass(this, SetGroupActivity::class.java)
        intent.putExtra(GJApp.MY_GROUP_ID, SpUtils.getInstance().get(Constants.groupIDLS, "").toString())
        startActivityForResult(intent, 100)
        this.finishActivity(100)
    }

}
